package com.tianxin.workbao.workBao.Activity;

import android.content.Context;
import android.os.Bundle;
import androidx.annotation.Nullable;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.appcompat.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Sampling;
import com.tianxin.workbao.workBao.bean.Testbill;
import com.tianxin.workbao.workBao.common.Constant;
import com.tianxin.workbao.workBao.dataProvide.SamplingProvide;
import com.tianxin.workbao.workBao.dataProvide.TestProvide;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.BeansUtil;
import com.tianxin.workbao.workBao.utils.common.SPUtils;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.util.Date;

import static com.tianxin.workbao.workBao.R.id.ash_value;
import static com.tianxin.workbao.workBao.R.id.car_number;
import static com.tianxin.workbao.workBao.R.id.coal_name;
import static com.tianxin.workbao.workBao.R.id.heat_value;
import static com.tianxin.workbao.workBao.R.id.net_weight;
import static com.tianxin.workbao.workBao.R.id.remark;
import static com.tianxin.workbao.workBao.R.id.sampling_cars;
import static com.tianxin.workbao.workBao.R.id.sampling_number;
import static com.tianxin.workbao.workBao.R.id.sampling_time;
import static com.tianxin.workbao.workBao.R.id.sampling_user;
import static com.tianxin.workbao.workBao.R.id.sulfur_value;
import static com.tianxin.workbao.workBao.R.id.water;

/**
 * Created by hacker on 2017/6/22.
 */

public class TestOptionActivity extends BaseActivity implements View.OnClickListener, UpdateListViewListener {
    private ImageView arrowImageView;
    private ExpandView expandView;
    private FloatingActionButton saveFab;
    private ImageButton deleteBtn;
    private TextView samplingNumberTV;
    private TextView samplingTimeTV;
    private TextView samplingCarsTV;
    private TextView samplingUserTV;
    private TextView netWeightTV;
    private TextView coalNameTV;
    private TextView carNumbersTV;
    private TextView remarkTV;
    private EditText waterET;
    private EditText ashValueET;
    private EditText sulfurValueET;
    private EditText heatvalueET;

    private Testbill mTestbill;
    private Sampling mSampling;

    private TestProvide testProvide;
    private SamplingProvide samplingProvide;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_bill_option);
        super.initStatusBar(true);

        initData();

        initView();

        initEvent();

        bindView();
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return super.onCreateView(name, context, attrs);
    }

    public void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("化验结果录入");
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(AppCompatResources.getDrawable(this, R.drawable.ic_action_back_24dp));
        saveFab = (FloatingActionButton) findViewById(R.id.save_fab);
        expandView = (ExpandView) findViewById(R.id.expandView);
        arrowImageView = (ImageView) findViewById(R.id.arrow);
        deleteBtn = (ImageButton) findViewById(R.id.delete_btn);
        samplingNumberTV = (TextView) findViewById(sampling_number);
        samplingTimeTV = (TextView) findViewById(sampling_time);
        samplingCarsTV = (TextView) findViewById(sampling_cars);
        samplingUserTV = (TextView) findViewById(sampling_user);
        netWeightTV = (TextView) findViewById(net_weight);
        coalNameTV = (TextView) findViewById(coal_name);
        carNumbersTV = (TextView) findViewById(car_number);
        remarkTV = (TextView) findViewById(remark);
        waterET = (EditText) findViewById(water);
        ashValueET = (EditText) findViewById(ash_value);
        sulfurValueET = (EditText) findViewById(sulfur_value);
        heatvalueET = (EditText) findViewById(heat_value);
    }

    @Override
    public void initData() {
        Bundle bundle = getIntent().getExtras();
        mSampling = (Sampling) bundle.getSerializable("sampling");
        mTestbill = new Testbill();
        mTestbill.setSamplingid((int) mSampling.getId());
        mTestbill.setSamplinguid(mSampling.getSamplinguid());
        mTestbill.setSamplingnum(mSampling.getSbnum());
        mTestbill.setSamplinguname(mSampling.getSamplinguname());
        mTestbill.setCoalfactory(mSampling.getCoalfactory());
        mTestbill.setAddtime(new Date());
        mTestbill.setIstest(1);
        mTestbill.setCoalfactoryid(mSampling.getCoalfactoryid());
        mTestbill.setContractid(mSampling.getContractid());
        mTestbill.setCtnum(mSampling.getCtnum());
        mTestbill.setCoaltype(mSampling.getCoaltype());
        mTestbill.setAdduserid((int) SPUtils.getPrefLong(Constant.CURRENT_USER_ID, 0));
        mTestbill.setAddusername(SPUtils.getPrefString(Constant.CURRENT_USER_NAME, "no"));
        mTestbill.setTesttime(mTestbill.getAddtime());
        mTestbill.setTestuid(mTestbill.getAdduserid());
        mTestbill.setTestuname(mTestbill.getAddusername());
        mTestbill.setWeighbillids(mSampling.getWeighbillids());
        mTestbill.setWeighbillnums(mSampling.getWeighbillnums());
        mTestbill.setWeights(mSampling.getWeights());
        mTestbill.setWeightscarnum(mSampling.getWeightscarnum());
        mTestbill.setTbnum(BeansUtil.getNumber(Constant.TEST_HEADER));
        mTestbill.setTbtype(mSampling.getSbtype());
        mTestbill.setSupplycpname(mSampling.getSupplycpname());
        mTestbill.setIsis(1);
        mTestbill.setStatus(1);
        if (mSampling.getRemark() != null) {
            mTestbill.setRemark(mSampling.getRemark());
        }
    }


    @Override
    public void initEvent() {
        //navigationIcon event
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TestOptionActivity.this.finish();
            }
        });
        arrowImageView.setOnClickListener(this);
        saveFab.setOnClickListener(this);
        deleteBtn.setOnClickListener(this);
    }

    @Override
    public void bindView() {
        samplingNumberTV.setText(mSampling.getSbnum());
        samplingTimeTV.setText(TimeUtils.parseDateTime(mSampling.getSamplingtime()));
        samplingCarsTV.setText(mSampling.getWeightscarnum() + "车");
        samplingUserTV.setText(mSampling.getSamplinguname());
        netWeightTV.setText(mSampling.getWeights() + "吨");
        coalNameTV.setText(mSampling.getCoaltype());
        carNumbersTV.setText(mSampling.getCarnumbers());
        if (mSampling.getRemark().length() < 2) {
            remarkTV.setVisibility(View.GONE);
        } else {
            remarkTV.setVisibility(View.VISIBLE);
            remarkTV.setText(mSampling.getRemark());
        }
    }

    @Override
    public void onBackPressed() {
        //alert
        super.onBackPressed();
    }

    @Override
    public void onClick(View view) {
        int viewId = view.getId();
        switch (viewId) {
            case R.id.arrow:
                if (expandView.isExpand()) {
                    expandView.collapse();
                    arrowImageView.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                } else {
                    expandView.expand();
                    arrowImageView.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                }
                break;
            case R.id.save_fab:
                saveTestBill();
                break;
            case R.id.delete_btn:
                deleteSamplingBill();
                break;
        }
    }

    /**
     * delete Sampling bill
     */
    private void deleteSamplingBill() {
        if (mSampling == null || mSampling.getId() == 0) {
            showMessage("无法删除");
            return;
        }
        getSamplingProvide().deleteSampling((int) mSampling.getId());
    }

    private void saveTestBill() {
        if (waterET.getText().length() == 0
                || ashValueET.getText().length() == 0
                || sulfurValueET.getText().length() == 0) {
            showMessage("所输入的化验值不完整");
            return;
        }
        mTestbill.setAnalyzewatervalue(Float.valueOf(waterET.getText().toString()));
        mTestbill.setAshvalue(Float.valueOf(ashValueET.getText().toString()));
        mTestbill.setTotalsulfurvalue(Float.valueOf(sulfurValueET.getText().toString()));
        if (heatvalueET.getText().length() > 0) {
            mTestbill.setHeatvalue(Float.valueOf(heatvalueET.getText().toString()));
        }
        //save
        getTestProvide().saveTest(mTestbill);
    }

    @Override
    public void showMessage(String msg) {
        if (msg == "ok") {
            getSweetAlertDialog(this).showSuccess("", "成功");
            this.finish();
        } else if (msg.contains("failure")) {
            Toast.makeText(this, "失败！\n " + msg.substring(msg.indexOf("failure") + 1, msg.length()), Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void showProgress() {
        getSweetAlertDialog(this).showDialog("",0);
    }

    @Override
    public void hideProgress() {
        getSweetAlertDialog(this).dismiss();
    }

    @Override
    public void onRefreshData(Object result) {

    }

    @Override
    public void onLoadMoreData(Object result) {

    }

    public TestProvide getTestProvide() {
        if (testProvide == null) {
            testProvide = new TestProvide(this, this);
        }
        return testProvide;
    }

    public SamplingProvide getSamplingProvide() {
        if (samplingProvide == null) {
            samplingProvide = new SamplingProvide(this, this, false);
        }
        return samplingProvide;
    }
}
